package regpang.listviewtest.DragRecycler;

import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import regpang.listviewtest.R;
import regpang.listviewtest.decoratio.DividerItemDecoration;

public class DragRecyclerActivity extends AppCompatActivity {
    List<Subject> datas = new ArrayList<>();
    private DragRecyclerAdapter dragRecyclerAdapter;
    private Toast toast;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linear);
        setTitle("可拖拽式列表");
        //字符串资源
        String[] stringArray = getResources().getStringArray(R.array.listData);
        Log.d("DragRecyclerActivity", "列表字符串资源数量:" + stringArray.length);
        //初始化data
        for (int i = 0; i < stringArray.length; i++) {
            //动态获取资源ID，第一个参数是资源名，第二个参数是资源类型例如drawable，string等，第三个参数包名
            int imageId = getResources().getIdentifier("ic_category_" + i, "mipmap", getPackageName());
            datas.add(new Subject(stringArray[i], imageId));
        }

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        /*网格*/
//        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
//        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        /*列表*/
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        dragRecyclerAdapter = new DragRecyclerAdapter(datas, this);
        recyclerView.setAdapter(dragRecyclerAdapter);

        ItemDragHelperCallback itemDragHelperCallback = new ItemDragHelperCallback(dragRecyclerAdapter);
        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragHelperCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
        recyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(recyclerView) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {

            }

            @Override
            public void onItemLongClick(RecyclerView.ViewHolder vh) {
                //获取系统震动服务
                Vibrator vib = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                //震动70毫秒
                vib.vibrate(70);
                //判断被拖拽的是否是前两个，如果不是则执行拖拽
                if (vh.getLayoutPosition() != 0 && vh.getLayoutPosition() != 1) {
                    itemTouchHelper.startDrag(vh);
                } else {
                    if (toast == null) {
                        toast = Toast.makeText(DragRecyclerActivity.this, "前两个不可拖拽", Toast.LENGTH_SHORT);
                    } else {
                        toast.cancel();
                        toast = Toast.makeText(DragRecyclerActivity.this, "前两个不可拖拽", Toast.LENGTH_SHORT);
                    }
                    toast.show();
                }
            }
        });
    }
}
